// pages/myfiledetail/myfiledetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileInfo: {},
    modalHidden: true, //修改文件信息
    
    file_id:null,
  },

  //页面初始化
  onLoad: function (options) {
    this.setData({
      file_id:parseInt(options.id),
    })
  },

  //页面刷新
  onShow:function(){
    var that = this
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_one_file',
      method: "POST", //请求方式
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        file_id: that.data.file_id,
      },
      success(res) {
        console.log(res.data.data)
        that.setData({
          fileInfo: res.data.data,
        })
      }
    })
  },

  //在线预览
  openFile: function () {
    var that = this

    wx.showLoading({
      title: '加载中...',
    })
    //根据https路径可以获得http格式的路径(指定文件下载后缓存的路径 (本地路径)),根据这个路径可以预览
    wx.downloadFile({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/down_file?file_id=' + that.data.fileInfo.file_id,
      success(res) {
        console.log(res)
        that.setData({
          httpfile: res.tempFilePath
        })
        //获取文件后缀
        const houzhui = res.tempFilePath.match(/\.[^.]+?$/)[0]; //文件后缀，不知道这串代码什么意思
        console.log(houzhui)
        if (houzhui == ".bin" || houzhui == ".unknown") {
          wx.hideLoading()
          wx.showModal({
            title: '温馨提示',
            content: '文件格式不支持预览',
            showCancel: false,
          })
        } else {
          //预览文件
          wx.openDocument({
            filePath: that.data.httpfile,
            showMenu: true,
            success: res => {
              wx.hideLoading()
              //console.log(res)
            },
            fail: err => {
              console.log(err);
              console.log(that.data.httpfile)
              wx.hideLoading()
              wx.showModal({
                title: '温馨提示',
                content: '文件格式不支持预览',
                showCancel: false,
              })
            }
          })
        }
      },
      fail: (err) => {
        console.log('读取失败', err)
      }
    })
  },

  //点击下载文件
  downLoad: function () {
    var that = this
    //复制下载链接到剪切板
    wx.setClipboardData({
      data: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/down_file?file_id=' + that.data.fileInfo.file_id,
      success: res => {
        wx.showToast({
          title: '复制成功',
          icon: 'success'
        })
      }
    })
  },

  //点击修改信息
  Modaltap: function () {
    this.setData({
      modalHidden: false
    })
  },

  //获取单选框的值
  getRadio(e) {
    console.log('单选框的值：', e.detail.value)
    this.setData({
      ['fileInfo.type']: e.detail.value
    })
  },
  //获取作者
  getAuthor(e) {
    console.log('备注输入框的值：', e.detail.value)
    this.setData({
      ['fileInfo.author']: e.detail.value,
    })
  },
  //获取备注
  getRemark(e) {
    console.log('备注输入框的值：', e.detail.value)
    this.setData({
      ['fileInfo.remark']: e.detail.value,
    })
  },
  //获取金币
  getGoldNum(e) {
    console.log('金币输入框的值：', e.detail.value)
    this.setData({
      ['fileInfo.gold_num']: e.detail.value,
    })
  },

  changeData:function(){
    var that=this
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/change_fileInfo',
      method: "POST", //请求方式
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data:{
        file_id:that.data.fileInfo.file_id,
        type: that.data.fileInfo.type, //类型
        author: that.data.fileInfo.author, //作者
        remark: that.data.fileInfo.remark, //备注
        gold_num: that.data.fileInfo.gold_num, //文件下载所需金币数
      },
      success(res){
        console.log(res.data)
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        })
        that.setData({
          modalHidden: true, //修改文件信息
        })
      }
    })
  }
})